import akshare as ak
from connector import mysqlConnectorHelper as mc
import datetime
import SimpleMySqlClass as smc


# 获取数据库链接
def get_mysql_conn():
    conn = smc.SimpleMySqlClass("127.0.0.1", "stocks", "haojg", "haojg")
    return conn


def check_and_create_table(conn, code, table_name):
    if not check_table(conn, table_name):
        base_table_name = table_name.replace("_" + code, "")
        create_like_table(conn, base_table_name, table_name)
    return


def check_table(conn, table_name):
    all_tables = conn.list_tables()
    for t in all_tables:
        cur_table_name = t[0]
        print("table = ", cur_table_name)
        if cur_table_name == table_name:
            return True
    return False


def create_like_table(conn, base_table_name, table_name):
    copy_sql = f"create table {table_name} like {base_table_name}"
    rs = conn.execute(copy_sql)
    return rs


def get_data_and_read(code,table_name):
    # 获取链接
    conn = get_mysql_conn()
    check_and_create_table(conn,code,table_name)

    # 关闭连接
    conn.close()


if __name__ == "__main__":
    gg_code_table_arr = [
        {"code": "002202", "table_name": "t_stock_k_data_002202", "desc": "jf"}
    ]

    for gct in gg_code_table_arr:
        code = gct["code"]
        table_name = gct["table_name"]
        desc = gct["desc"]
        print(f"开始获取{ desc} 数据, code = {code}, table_name={table_name}")
        get_data_and_read(code, table_name)
    print("end")
